package com.glory.good_dark.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.good_dark.R;
import com.glory.good_dark.application.App;
import com.glory.good_dark.entity.CheckShowEntity;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DateUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * FileName: CheckShowListAdapter
 * Author: XJH
 * Date: 2020/4/14 15:14
 * Description:
 * History:
 */
public class CheckShowListAdapter extends BaseQuickAdapter<CheckShowEntity.CheckPam, BaseViewHolder> {

    private List<CheckShowEntity.CheckItem> checkNameList;
    private List<CheckShowEntity.CheckHisData> checkValueList;
    private List<String> outData;
    private String guid;
    private List<String> valueList;
    private FreeUI_GeneralPop valuePop;

    public CheckShowListAdapter(@Nullable List<CheckShowEntity.CheckPam> data) {
        super(R.layout.item_check_show, data);
        outData = new ArrayList<>();
        valueList = new ArrayList<>();
        valueList.add("是");
        valueList.add("否");
    }

    @Override
    protected void convert(BaseViewHolder helper, CheckShowEntity.CheckPam item) {
        helper.setText(R.id.tvPosition,item.getCHECK_N0());
        helper.setText(R.id.tvProjectName,item.getPARAMETER_NAME());
        helper.setGone(R.id.etValue,true);
        helper.setGone(R.id.tvValue,false);
        helper.setGone(R.id.etRemarks,true);
        helper.setGone(R.id.tvRemarks,false);
        EditText etValue = helper.getView(R.id.etValue);
        EditText etRemarks = helper.getView(R.id.etRemarks);
        setEditTextListener(etRemarks,item);
        item.setCHECKID(guid);
//        item.setUPDATED_TIME(FreeApi_DateUtils.getNowStringDate("yyyy/MM/dd HH:mm:ss"));
        for (CheckShowEntity.CheckItem nameData : checkNameList){
            if (nameData.getPARAMETER_NAME().equals(item.getPARAMETER_NAME())){
                //选择框/输入框
                if (nameData.getPARAMETER_TYPE().equals("选择框")){
                    helper.setGone(R.id.etValue,false);
                    helper.setGone(R.id.flValueList,true);
                }
                helper.setText(R.id.etValue,nameData.getParameter_Value());
                // 上下限控制
                setEditListener(helper,etValue,nameData.getTOP_LIMIT(),nameData.getBOTTOM_LIMIT(),item,helper.getAdapterPosition());
            }
        }

        if (checkValueList.size() <= 0){
            item.setID("0");
            helper.setGone(R.id.tvConfirm,false);
            helper.setGone(R.id.ivConfirm,true);
        }else {
            for (CheckShowEntity.CheckHisData hisData : checkValueList){
                if (hisData.getPARAMETER_NAME().equals(item.getPARAMETER_NAME())){
                    if (FreeApi_StringUtils.isEmpty(hisData.getID())){
                        item.setID("0");
                    }else {
                        item.setID(hisData.getID());
                    }
                    item.setCREATOR(hisData.getCREATOR());
                    item.setCREATED_TIME(hisData.getCREATED_TIME());
                    item.setUPDATOR(hisData.getUPDATOR());
//                    item.setUPDATED_TIME(hisData.getUPDATED_TIME());
                    item.setCHECK_NAME(hisData.getCHECK_NAME());
                    item.setPARAMETER_VALUE(hisData.getPARAMETER_VALUE());
                    helper.setText(R.id.etRemarks,hisData.getDESCRIPTION());
                    helper.setText(R.id.etValue,hisData.getPARAMETER_VALUE());
                    //执行人，确认人
                    helper.setText(R.id.tvExecute,hisData.getOPERATOR());
                    item.setOPERATOR(hisData.getOPERATOR());
                    if (!FreeApi_StringUtils.isEmpty(hisData.getCONFIRM_PERSON())){
                        helper.setText(R.id.tvConfirm,hisData.getCONFIRM_PERSON());
                        item.setCONFIRM_PERSON(hisData.getCONFIRM_PERSON());
                    }else {
                        helper.setGone(R.id.tvConfirm,false);
                        helper.setGone(R.id.ivConfirm,true);
                    }

                }
            }
        }

        ImageView ivConfirm = helper.getView(R.id.ivConfirm);
        ivConfirm.setOnClickListener(v -> {
            ivConfirm.setVisibility(View.GONE);
            helper.setText(R.id.tvConfirm,App.userEntity.getNAME());
            item.setCONFIRM_PERSON(App.userEntity.getNAME());
            helper.setGone(R.id.tvConfirm,true);
        });

        FrameLayout flValueList = helper.getView(R.id.flValueList);
        flValueList.setOnClickListener(v -> {
            valuePop = FreeApi_PopUtils.getNormalPop(App.app, flValueList.getWidth(), valueList, helper.getView(R.id.ivValueArrow), (code, i) -> {
                helper.setText(R.id.tvValues,code);
                item.setPARAMETER_VALUE(code.equals("是") ? "1" : "0");
                helper.setText(R.id.tvExecute,App.userEntity.getNAME());
                item.setOPERATOR(App.userEntity.getNAME());

            });
            FreeApi_PopUtils.setArrowShow(helper.getView(R.id.ivValueArrow));
            valuePop.showAsDropDown(flValueList, 0, 0);
        });
    }

    private void setEditListener(BaseViewHolder helper,EditText editText, String topLimit, String bottomLimit, CheckShowEntity.CheckPam item,int position) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }

        TextWatcher tw = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(s.length()>0)
                {
//                    try {
//                        double value = Double.parseDouble(s.toString());
//                        if (helper.getView(R.id.flValueList).getVisibility() == View.VISIBLE){
//                            return;
//                        }
//                        compare(value,editText,topLimit,bottomLimit,position);
//                    } catch (NumberFormatException e) {
//                        e.printStackTrace();
//                        editText.setTextColor(App.app.getResources().getColor(R.color.red));
//                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                item.setPARAMETER_VALUE(s.toString());
            }

        };

        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!editText.getText().toString().equals("")){
                    helper.setText(R.id.tvExecute,App.userEntity.getNAME());
                    item.setOPERATOR(App.userEntity.getNAME());
                }
            }
        });

        editText.addTextChangedListener(tw);
        editText.setTag(tw);
    }

    private void setEditTextListener(EditText editText,CheckShowEntity.CheckPam item){
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }

        TextWatcher tw = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(s.length()>0)
                {
                    item.setDESCRIPTION(s.toString());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }

        };

        editText.addTextChangedListener(tw);
        editText.setTag(tw);
    }

    public void setCheckNameList(List<CheckShowEntity.CheckItem> list){
        this.checkNameList = list;
    }

    public void setCheckValueList(List<CheckShowEntity.CheckHisData> list){
        this.checkValueList = list;
    }

    public void setCheckID(String guid){
        this.guid = guid == null ? "" : guid;
    }

    public List<String> getOutData(){
        return outData;
    }

    private void compare(double value, EditText editText,String maxValue,String minValue,int position){
        if (!FreeApi_StringUtils.isEmpty(maxValue)&&!FreeApi_StringUtils.isEmpty(minValue)){
            double maxValues = Double.parseDouble(maxValue);
            double minValues = Double.parseDouble(minValue);
            if(minValues<=value&&value<=maxValues) {
                editText.setTextColor(App.app.getResources().getColor(R.color.black));
                if (outData.contains(String.valueOf(position))){
                    outData.remove(String.valueOf(position));
                }
            }else{
                editText.setTextColor(App.app.getResources().getColor(R.color.red));
                if (!outData.contains(String.valueOf(position))){
                    outData.add(String.valueOf(position));
                }
            }
        }else if (FreeApi_StringUtils.isEmpty(maxValue)&&!FreeApi_StringUtils.isEmpty(minValue)){
            double minValue1 = Double.parseDouble(minValue);
            if (value >= minValue1){
                editText.setTextColor(App.app.getResources().getColor(R.color.black));
                if (outData.contains(String.valueOf(position))){
                    outData.remove(String.valueOf(position));
                }
            }else{
                editText.setTextColor(App.app.getResources().getColor(R.color.red));
                if (!outData.contains(String.valueOf(position))){
                    outData.add(String.valueOf(position));
                }
            }
        }else if (!FreeApi_StringUtils.isEmpty(maxValue)&&FreeApi_StringUtils.isEmpty(minValue)){
            double maxValue1 = Double.parseDouble(maxValue);
            if (value <= maxValue1){
                editText.setTextColor(App.app.getResources().getColor(R.color.black));
                if (outData.contains(String.valueOf(position))){
                    outData.remove(String.valueOf(position));
                }
            }else{
                editText.setTextColor(App.app.getResources().getColor(R.color.red));
                if (!outData.contains(String.valueOf(position))){
                    outData.add(String.valueOf(position));
                }
            }
        }else {
            editText.setTextColor(App.app.getResources().getColor(R.color.black));
        }
    }
}
